﻿#include "GUIContourPlotDialog.h"
#include "ui_GUIContourPlotDialog.h"
#include "FITK_Component/FITKAcousticsPostData/FITKAcousticsContourPlotData.h"

GUI::GUIContourPlotDialog::GUIContourPlotDialog(QWidget *parent) :
    Core::FITKDialog(parent),
    _ui(new Ui::GUIContourPlotDialog)
{
    _ui->setupUi(this);
    // 去掉帮助按钮
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    //关联信号槽
    connect(_ui->checkBox_Display, SIGNAL(clicked()), this, SLOT(updateControlStateByDisplay_Slot()));

    connect(_ui->checkBox_Flexure_X, SIGNAL(clicked()), this, SLOT(updateControlStateByBeamsXYE_Slot()));
    connect(_ui->checkBox_Flexure_Y, SIGNAL(clicked()), this, SLOT(updateControlStateByBeamsXYE_Slot()));
    connect(_ui->checkBox_Extension, SIGNAL(clicked()), this, SLOT(updateControlStateByBeamsXYE_Slot()));
    connect(_ui->checkBox_Torsion, SIGNAL(clicked()), this, SLOT(updateControlStateByBeamsT_Slot()));

    connect(_ui->comboBox_Hz, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTitle_Slot(int)));
    connect(_ui->comboBox_REsultType, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTitle_Slot(int)));

    QRegExp rExp = QRegExp("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$");
    _ui->lineEdit_Pressure_Ref->setValidator(new QRegExpValidator(rExp));
    _ui->lineEdit_Pressure_Min->setValidator(new QRegExpValidator(rExp));
    _ui->lineEdit_Pressure_Max->setValidator(new QRegExpValidator(rExp));
    _ui->lineEdit_Velocity_Ref->setValidator(new QRegExpValidator(rExp));
    _ui->lineEdit_Velocity_Min->setValidator(new QRegExpValidator(rExp));
    _ui->lineEdit_Velocity_Max->setValidator(new QRegExpValidator(rExp));


}

GUI::GUIContourPlotDialog::~GUIContourPlotDialog()
{
    //销毁
    delete _ui;
}

void GUI::GUIContourPlotDialog::on_pushButton_Apply_clicked()
{
    //保存数据
    saveData();
    //触发信号
    emit dialogApplySignal();
}

void GUI::GUIContourPlotDialog::on_pushButton_OK_clicked()
{
    //保存数据
    saveData();
    //修改状态
    this->accept();
    //关闭窗口
    close();
}

void GUI::GUIContourPlotDialog::on_pushButton_Close_clicked()
{
    //关闭窗口
    close();
}

void GUI::GUIContourPlotDialog::updateControlStateByDisplay_Slot()
{
    //根据显示状态 更新控件可用状态
    bool b = _ui->checkBox_Display->isChecked();

    _ui->radioButton_At->setEnabled(b);
    _ui->radioButton_OverAllLevel->setEnabled(b);
    _ui->comboBox_LoadCase->setEnabled(b);
    _ui->comboBox_REsultType->setEnabled(b);
    _ui->comboBox_EngUnitsType->setEnabled(b);
    _ui->comboBox_Component->setEnabled(b);
    _ui->comboBox_Location->setEnabled(b);
    _ui->comboBox_Quantity->setEnabled(b);
    _ui->comboBox_Hz->setEnabled(b);

    //刷新标题
    updateTitle_Slot(0);
}

void GUI::GUIContourPlotDialog::updateControlStateByBeamsXYE_Slot()
{
    blockSignals(true);
    // XYE 与 T 互斥
    bool b = _ui->checkBox_Flexure_X->isChecked();
    b |= _ui->checkBox_Flexure_Y->isChecked();
    b |= _ui->checkBox_Extension->isChecked();

    if (b) _ui->checkBox_Torsion->setChecked(!b);

    blockSignals(false);
}

void GUI::GUIContourPlotDialog::updateControlStateByBeamsT_Slot()
{
    blockSignals(true);
    // XYE 与 T 互斥
    if (_ui->checkBox_Torsion->isChecked())
    {
        _ui->checkBox_Flexure_X->setChecked(false);
        _ui->checkBox_Flexure_Y->setChecked(false);
        _ui->checkBox_Extension->setChecked(false);
    }
    blockSignals(false);
}

void GUI::GUIContourPlotDialog::init()
{
    //清空数据
    clearControlData();
    //临时默认初始化
    _listEngUnitsType << "Pressure";
    _listEngUnitsType << "Velocity";

    //临时 后期换掉
    _ui->comboBox_EngUnitsType->addItems(_listEngUnitsType);
    _ui->comboBox_REsultType->addItem("Engineering Units");
    _ui->comboBox_Quantity->addItem("Magnitude");
    _ui->comboBox_LoadCase->addItems(_listLoadCases);
    int index = 0;
    for (float f : _listHz)
    {
        _ui->comboBox_Hz->addItem(QString::number(f), index);
        index++;
    }
    //加载数据
    loadData();
}

void GUI::GUIContourPlotDialog::updateScaleState()
{
    loadDataScaling();
}

void GUI::GUIContourPlotDialog::updatePressureUnits()
{
    QString strUnit = "Pa";
    if (_ui->checkBox_Pressure_A->isChecked())
    {
        strUnit = "dBA";
    }
    else if (_ui->checkBox_Pressure_DB->isChecked())
    {
        strUnit = "dB";
    }

    _ui->label_Pressure_Max->setText(strUnit);
    _ui->label_Pressure_Min->setText(strUnit);
}

void GUI::GUIContourPlotDialog::loadData()
{
    //加载标签页数据
    loadDataGeneral();
    loadDataScaling();
    loadDataWaveFields();
    //刷新标题
    updateTitle_Slot(0);
}

void GUI::GUIContourPlotDialog::loadDataGeneral()
{
    if (nullptr == _plotData) return;
    //General
    _ui->checkBox_Display->setChecked(_plotData->getDisplay());
    updateControlStateByDisplay_Slot();
    _ui->comboBox_Hz->setCurrentIndex(_plotData->getSelectedHzIndex());
    _ui->comboBox_LoadCase->setCurrentText(_plotData->getSelectedLoadCase());//后期更换
    _ui->comboBox_EngUnitsType->setCurrentIndex(int(_plotData->getSelectedEngUnitsType()));//后期更换

}

void GUI::GUIContourPlotDialog::loadDataScaling()
{
    if (nullptr == _plotData) return;
    //Scaling
    _ui->radioButton_Velocity_Log->setChecked(_plotData->getVelocity_IsLog());
    _ui->radioButton_Velocity_Linear->setChecked(!_plotData->getVelocity_IsLog());
    _ui->checkBox_Velocity_DB->setChecked(_plotData->getVelocity_DB());
    _ui->checkBox_Velocity_Auto->setChecked(_plotData->getVelocity_Autoscale());
    _ui->lineEdit_Velocity_Ref->setText(QString::number(_plotData->getVelocity_Ref()));
    _ui->lineEdit_Velocity_Min->setText(QString::number(_plotData->getVelocity_Min()));
    _ui->lineEdit_Velocity_Max->setText(QString::number(_plotData->getVelocity_Max()));
    //初始化
    on_checkBox_Velocity_DB_clicked();

    _ui->radioButton_Pressure_Log->setChecked(_plotData->getPressure_IsLog());
    _ui->radioButton_Pressure_Linear->setChecked(!_plotData->getPressure_IsLog());
    _ui->checkBox_Pressure_DB->setChecked(_plotData->getPressure_DB());
    _ui->checkBox_Pressure_Auto->setChecked(_plotData->getPressure_Autoscale());
    _ui->lineEdit_Pressure_Ref->setText(QString::number(_plotData->getPressure_Ref()));
    _ui->lineEdit_Pressure_Min->setText(QString::number(_plotData->getPressure_Min()));
    _ui->lineEdit_Pressure_Max->setText(QString::number(_plotData->getPressure_Max()));
    //初始化
    on_checkBox_Pressure_DB_clicked();

}

void GUI::GUIContourPlotDialog::loadDataWaveFields()
{
    if (nullptr == _plotData) return;
    //WaveFields
    _ui->checkBox_Transverse_Flexure->setChecked(_plotData->getTransverse_Flexure());
    _ui->checkBox_In_plane_Extension->setChecked(_plotData->getIn_plane_Extension());
    _ui->checkBox_In_Plane_Shear->setChecked(_plotData->getIn_plane_Shear());

    _ui->checkBox_Flexure_X->setChecked(_plotData->getBeams_Flexure_X());
    _ui->checkBox_Flexure_Y->setChecked(_plotData->getBeams_Flexure_Y());
    _ui->checkBox_Extension->setChecked(_plotData->getBeams_Extension());
    _ui->checkBox_Torsion->setChecked(_plotData->getBeams_Torsion());

}

void GUI::GUIContourPlotDialog::saveData()
{
    //保存标签页数据
    saveDataGeneral();
    saveDataScaling();
    saveDataWaveFields();
}

void GUI::GUIContourPlotDialog::saveDataGeneral()
{
    if (nullptr == _plotData) return;
    //General
    _plotData->setDisplay(_ui->checkBox_Display->isChecked());
    _plotData->setSelectedHzIndex(_ui->comboBox_Hz->currentIndex());
    _plotData->setSelectedHzName(_ui->comboBox_Hz->currentText());
    _plotData->setSelectedLoadCase(_ui->comboBox_LoadCase->currentText());
    _plotData->setSelectedEngUnitsType(Acoustics::FITKAcousticsContourPlotData::EngUnitsType(_ui->comboBox_EngUnitsType->currentIndex()));
    _plotData->setSelectEngUnitsTypeName(_ui->comboBox_EngUnitsType->currentText());

}

void GUI::GUIContourPlotDialog::saveDataScaling()
{
    if (nullptr == _plotData) return;
    //Scaling
    _plotData->setVelocity_IsLog(_ui->radioButton_Velocity_Log->isChecked());
    _plotData->setVelocity_DB(_ui->checkBox_Velocity_DB->isChecked());
    _plotData->setVelocity_Autoscale(_ui->checkBox_Velocity_Auto->isChecked());
    _plotData->setVelocity_Ref(_ui->lineEdit_Velocity_Ref->text().toDouble());
    _plotData->setVelocity_Min(_ui->lineEdit_Velocity_Min->text().toDouble());
    _plotData->setVelocity_Max(_ui->lineEdit_Velocity_Max->text().toDouble());

    _plotData->setPressure_IsLog(_ui->radioButton_Pressure_Log->isChecked());
    _plotData->setPressure_DB(_ui->checkBox_Pressure_DB->isChecked());
    _plotData->setPressure_Autoscale(_ui->checkBox_Pressure_Auto->isChecked());
    _plotData->setPressure_AWeighting(_ui->checkBox_Pressure_A->isChecked());
    _plotData->setPressure_Ref(_ui->lineEdit_Pressure_Ref->text().toDouble());
    _plotData->setPressure_Min(_ui->lineEdit_Pressure_Min->text().toDouble());
    _plotData->setPressure_Max(_ui->lineEdit_Pressure_Max->text().toDouble());
}

void GUI::GUIContourPlotDialog::saveDataWaveFields()
{
    if (nullptr == _plotData) return;
    //WaveFields
    _plotData->setTransverse_Flexure(_ui->checkBox_Transverse_Flexure->isChecked());
    _plotData->setIn_plane_Extension(_ui->checkBox_In_plane_Extension->isChecked());
    _plotData->setIn_plane_Shear(_ui->checkBox_In_Plane_Shear->isChecked());

    _plotData->setBeams_Flexure_X(_ui->checkBox_Flexure_X->isChecked());
    _plotData->setBeams_Flexure_Y(_ui->checkBox_Flexure_Y->isChecked());
    _plotData->setBeams_Extension(_ui->checkBox_Extension->isChecked());
    _plotData->setBeams_Torsion(_ui->checkBox_Torsion->isChecked());

}

void GUI::GUIContourPlotDialog::clearControlData()
{
    //清空控件数据
    _ui->comboBox_Hz->clear();
    _ui->comboBox_LoadCase->clear();
    _ui->comboBox_EngUnitsType->clear();
    _ui->comboBox_REsultType->clear();
    _ui->comboBox_Quantity->clear();
}

void GUI::GUIContourPlotDialog::updateTitle_Slot(int i)
{
    Q_UNUSED(i);
    //根据不同状态 生成标题内容
    QString strTitle = tr("Contour Plot - %1 Hz ")
        .arg(_ui->comboBox_Hz->currentText());
    if (_ui->checkBox_Display->isChecked())
    {
        strTitle.append(QString("- %1").arg(_ui->comboBox_REsultType->currentText()));
    }
    this->setWindowTitle(strTitle);
}

void GUI::GUIContourPlotDialog::on_checkBox_Velocity_DB_clicked()
{
    //联动
    bool b = _ui->checkBox_Velocity_DB->isChecked();
    _ui->radioButton_Velocity_Log->setEnabled(!b);
    _ui->radioButton_Velocity_Linear->setEnabled(!b);
    _ui->lineEdit_Velocity_Ref->setEnabled(b);

    QString strUnit = b ? "dB" : "m/s";

    _ui->label_Velocity_Max->setText(strUnit);
    _ui->label_Velocity_Min->setText(strUnit);

}

void GUI::GUIContourPlotDialog::on_checkBox_Pressure_DB_clicked()
{
    //联动
    bool b = _ui->checkBox_Pressure_DB->isChecked();
    _ui->radioButton_Pressure_Log->setEnabled(!b);
    _ui->radioButton_Pressure_Linear->setEnabled(!b);
    _ui->lineEdit_Pressure_Ref->setEnabled(b);

    //DB状态下 可用A计权
    _ui->checkBox_Pressure_A->setEnabled(b);
    if (!b)
    {
        //DB未选中状态下  A计权也取消选中状态
        _ui->checkBox_Pressure_A->setChecked(b);
    }
    updatePressureUnits();
}

void GUI::GUIContourPlotDialog::on_checkBox_Pressure_A_clicked()
{
    updatePressureUnits();
}

void GUI::GUIContourPlotDialog::on_comboBox_EngUnitsType_currentIndexChanged(int index)
{
    //刷新标签页可用状态
    _ui->groupBox_Pressure->setEnabled(false);
    _ui->groupBox_Velocity->setEnabled(false);
    if (Acoustics::FITKAcousticsContourPlotData::EngUnitsType(index) == Acoustics::FITKAcousticsContourPlotData::EngUnitsType::Pressure)
    {
        _ui->groupBox_Pressure->setEnabled(true);
    }
    if (Acoustics::FITKAcousticsContourPlotData::EngUnitsType(index) == Acoustics::FITKAcousticsContourPlotData::EngUnitsType::Velocity)
    {
        _ui->groupBox_Velocity->setEnabled(true);
    }
}

